﻿Imports System
Imports System.Collections
Imports System.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class ReadonlyTypeDescriptor
        Inherits CustomTypeDescriptor
        ' Methods
        Friend Sub New(ByVal realTypeDescriptor As ICustomTypeDescriptor)
            MyBase.New(realTypeDescriptor)
        End Sub

        Public Overrides Function GetAttributes() As AttributeCollection
            Dim list As New ArrayList
            Dim attribute As Attribute
            For Each attribute In MyBase.GetAttributes
                If (Not TypeOf attribute Is EditorAttribute AndAlso Not TypeOf attribute Is ReadOnlyAttribute) Then
                    list.Add(attribute)
                End If
            Next
            list.Add(New ReadOnlyAttribute(True))
            Return New AttributeCollection(DirectCast(list.ToArray(GetType(Attribute)), Attribute()))
        End Function

        Public Overrides Function GetEvents(ByVal attributes As Attribute()) As EventDescriptorCollection
            Dim events As EventDescriptorCollection = MyBase.GetEvents(attributes)
            Dim list As New ArrayList
            Dim descriptor As EventDescriptor
            For Each descriptor In events
                Dim attribute As BrowsableAttribute = [TryCast](descriptor.Attributes.Item(GetType(BrowsableAttribute)),BrowsableAttribute)
                If ((Not attribute Is Nothing) AndAlso attribute.Browsable) Then
                    list.Add(New ReadonlyEventDescriptor(descriptor))
                Else
                    list.Add(descriptor)
                End If
            Next
            Return New EventDescriptorCollection(DirectCast(list.ToArray(GetType(EventDescriptor)), EventDescriptor()))
        End Function

        Public Overrides Function GetProperties() As PropertyDescriptorCollection
            Dim properties As PropertyDescriptorCollection = MyBase.GetProperties
            Dim list As New ArrayList
            Dim descriptor As PropertyDescriptor
            For Each descriptor In properties
                Dim attribute As BrowsableAttribute = [TryCast](descriptor.Attributes.Item(GetType(BrowsableAttribute)),BrowsableAttribute)
                If (((Not attribute Is Nothing) AndAlso attribute.Browsable) AndAlso Not TypeOf descriptor Is ReadonlyPropertyDescriptor) Then
                    list.Add(New ReadonlyPropertyDescriptor(descriptor))
                Else
                    list.Add(descriptor)
                End If
            Next
            Return New PropertyDescriptorCollection(DirectCast(list.ToArray(GetType(PropertyDescriptor)), PropertyDescriptor()))
        End Function

    End Class
End Namespace

